﻿Imports System.Text
Imports System.Windows.Controls
Imports System.Windows.Input
Imports System.Windows

Namespace VsSpeakerCompanion.UI
	Friend Class WidgetsControlPresenter
		Implements IWidget
        Private Const DefaultPaddingBetweenWidgets = 5.0

		Private context As Context
		Private view As WidgetsControl
        Private widgets As New List(Of IWidget)

		Friend Sub New(ByVal context As Context)
			Me.context = context
            Me.view = New WidgetsControl
		End Sub

		Friend Sub Add(ByVal widget As IWidget)
            If Not Me.widgets.Contains(widget) Then
                Me.widgets.Add(widget)

                If widget.VisualElement IsNot Nothing Then
                    Me.view.Add(widget)

                    ' Set the default padding to the widget (so it will be separated from the other widgets)
                    widget.VisualElement.SetValue(Control.PaddingProperty, New Thickness(DefaultPaddingBetweenWidgets))
                    ' Subscribe to the widget visual element events
                    AddHandler widget.VisualElement.MouseDown, AddressOf OnWidgetVisualElementMouseDown
                    AddHandler widget.VisualElement.MouseMove, AddressOf OnWidgetVisualElementMouseMove
                    AddHandler widget.VisualElement.KeyDown, AddressOf OnwidgetVisualElementKeyDown
                    AddHandler widget.VisualElement.KeyUp, AddressOf OnwidgetVisualElementKeyUp
                End If
            End If
		End Sub

		''' <summary>
		''' See <see cref="IWidget"/> for more information
		''' </summary>
		Public Sub Reset() Implements IWidget.Reset
			Me.widgets.ForEach(Sub(widget) widget.Reset())
		End Sub

		''' <summary>
		''' See <see cref="IWidget"/> for more information
		''' </summary>
        Public ReadOnly Property VisualElement As System.Windows.UIElement Implements IWidget.VisualElement
            Get
                Return Me.view
            End Get
        End Property

        Private Sub OnwidgetVisualElementKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
            e.Handled = True
        End Sub

		Private Sub OnwidgetVisualElementKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
			e.Handled = True
		End Sub

		Private Sub OnWidgetVisualElementMouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
			e.Handled = True
		End Sub

		Private Sub OnWidgetVisualElementMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
			e.Handled = True
		End Sub
	End Class
End Namespace